<template>
    <view class="body">
		<view class="face-wrapper">
			<image class="face" src="../../static/logo.png"></image>
		</view>        
        <form @submit="formlogin">
			<view class="info-wrapper line">
				<input name="username" type="text" class="input" :value="uname" placeholder="用户名" />
			</view>
			<view class="info-wrapper line">
				<input name="password" type="text" password="true" class="input" :value="pwd" placeholder="密码">
			</view>
			<view class="info-wrapper">
				<label class="checkbox-input" for="localLogin" @tap="changeSelect">					
					<checkbox name="localLogin" v-model="islocal"></checkbox>
					离线登录
				</label>
			</view>
			<view class="uni-btn-v">				
				<button type="primary" form-type="submit">登录</button>  
			</view>          
        </form>
	<!-- 	<view class="init-wrapper">
			<view class="btn-wrapper">				
				<button type="default" @tap="initData">重置本地数据</button>
			</view>
		</view>	  -->
    </view>
</template>

<script>	
export default {
    data() {
		return {
			islocal:false,
			uname:'',
			pwd:''
		};
	},
	onShow(){
		var username=uni.getStorageSync("username");
		if(username!=undefined || username!=''){
			this.uname=username;
		}
		var password=uni.getStorageSync("password");
		if(password!=undefined || password!=''){
			this.pwd=password;
		}
	},
    methods: {
		changeSelect(){
			this.islocal=!this.islocal
		},
        formlogin(e) {	
			uni.showLoading({
				title: '登录中...'
			});
            let data = {
					username: e.detail.value.username,
					password: e.detail.value.password
				};	
			
			if(!this.islocal){
				this.$common.isWifiExec(()=>{
					this.$config.SysConfig.APP_ISONLINE=true;
					this.$http.login(data)
					.then(data=>{
						uni.hideLoading();
						if(data.code.toString()!="1"){
							uni.showToast({
								icon:'none',
								title:data.msg,
								duration:1500
							})
						}
						else{							
							this.$common.loginSuccess(data)
						}
					});
				},()=>{
					uni.hideLoading();
					// uni.showToast(this.$config.msgs.needwifi)
					uni.showToast({
						title:this.$config.msgs.needwifi,
						icon:'none',
						duration:1500
					})
				})				
			} else {
				this.$config.SysConfig.APP_ISONLINE=false;
				this.$local.login(data,(res)=>{
					uni.hideLoading();					
					this.$common.loginSuccess(res)
				},(err)=>{
					uni.hideLoading();
					uni.showToast({
						icon:'none',
						title:err.msg,
						duration:1500
					})
				})
			}
        	uni.setStorageSync("username", data.username);
			uni.setStorageSync("password", data.password);
		},
		initData(){
			this.$common.initDBSchema(()=>{
				uni.showToast({
					icon:'none',
					title:this.$config.msgs.initOK,
					duration:1500
				})
				// uni.showToast(this.$config.msgs.initOK)
			})
		}
    }
};
</script>
<style>
	@import url("login.css");
</style>